<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> 
  <title>乐直播-硬件管理</title> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/layui/css/layui.css?cdnv=0.16"> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/portal/css/le-main24.css?cdnv=0.16"> 
  <link rel="stylesheet" href="http://cdn.lezhibo.com/static/libs/font-awesome/css/font-awesome.min.css?cdnv=0.16"> 
  <style type="text/css">
    .le-marketing-table tr{
    border-top: 1px solid #eaeaea;
    }
    .layui-textarea {width: 100%;}

    .le-marketing-table{
    margin:20px 0 10px 10px;
    }
        </style> 
 </head> 
 <body> 
  <form id="form" class="layui-form le-layer-from" action="${ctx }/portal/device/doSave.vo"> 
   <input type="hidden" name="device.id" value="f964ae51e5004eb289ad48028dddc9e5"> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">设备名称</label> 
    <div class="layui-input-block"> 
     <input type="text" validate="empty" name="device.name" value="虚拟设备2222222" lay-verify="title" autocomplete="off" placeholder="请输入设备名称" class="layui-input"> 
    </div> 
    <div class="tips" style="color: #999;margin-left: 95px; ">
     提示：建议设置不要超过10个字的名称
    </div> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">设备简介</label> 
    <div class="live-checkbox"> 
     <textarea name="device.description" validate="empty" placeholder="请输入简介" class="layui-textarea">测试~~</textarea> 
    </div> 
    <div class="tips" style="color: #999;margin-left: 95px; ">
     提示：建议设置不要超过45个字的简介
    </div> 
   </div> 
   <div class="layui-form-item"> 
    <label class="layui-form-label">设备图片</label> 
    <div class="layui-input-block"> 
     <div class="le-hardware-layer-image"> 
      <img class="le-avatar" src="http://cdn.lezhibo.com/attachment/20170816/b9b5802b704e45929c54824f14fcb837.png"> 
     </div> 
     <div class="le-hardware-layer-upload"> 
      <input type="file" name="file" class="layui-upload-file" lay-title="选择"> 
      <input type="hidden" class="avatar" name="device.avatar" value="/attachment/20170816/b9b5802b704e45929c54824f14fcb837.png"> 
     </div> 
    </div> 
    <div class="tips" style="color: #999;margin-left: 95px; padding-top: 100px">
     提示：建议上传分辨率为120*180，大小2M以内，格式为PNG、JPG、JPEG等常用格式图片
    </div> 
   </div> 
   <div class="le-hardware-layer-btn"> 
    <button type="button" class="layui-btn le-doSubmit">确定</button> 
   </div> 
  </form> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/libs/jquery/jquery.min.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/libs/jquery/jquery.form.min.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/layui/layui.js?cdnv=0.16"></script> 
  <script type="text/javascript" src="http://cdn.lezhibo.com/static/layui/lay/dest/layui.all.js?cdnv=0.16"></script> 
  <script>

        var cdnPath="http://cdn.lezhibo.com";//cdn路径
        function openLayer(url, title, func) {
            parent.openLayer(url,title,func);
        }
        function layerInit() {
            $("[layer-href]").click(function () {
                openLayer($(this).attr("layer-href"), $(this).attr("layer-title"), $(this).attr("layer-func"))
            });
        }

        $("[confirm-href]").click(function () {
            var href = $(this).attr("confirm-href");
            if ((typeof(href) == "undefined") || href == null || href == "") {
                return;
            }

            var title = $(this).attr("confirm-title");
            if ((typeof(title) == "undefined") || title == null || title == "") {
                title = "您确定执行该操作吗？"
            }
            layer.confirm(title, {icon: 9, title: '确定操作 ？'}, function (index) {
                        layer.close(index);
                        $.get(href, function (result) {
                            if (result.isOk) {
                                location.reload();
                            } else {
                                alert(result.message);
                            }
                        });
                    },
                    function (index) {
                        layer.close(index);
                    });
        });

        var verification ={
            mobiel:function(){
                $mobile = $("input[validate='mobile']").val();
                var re = /^1\d{10}$/;
                if((typeof($mobile)!="undefined") && !re.test($mobile)){
                    $("input[validate='mobile']").after('<p class="le-tips"><span style="color: red;width: 100%">请输入正确的手机号</span></p>');
                    return false;
                }
            },
            empty:function(){
                var $input =$("input[validate='empty']");
                for(var i=0;i<$input.length;i++){
                    if($input.eq(i).val().trim().length==0){
                        $("input[validate='empty']").eq(i).after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span></p>');
                        return false;
                    }
                }

                var $textarea =$("textarea[validate='empty']");
                for(var i=0;i<$textarea.length;i++){
                    if($textarea.eq(i).val().trim().length==0){
                        $("textarea[validate='empty']").eq(i).parent().after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span>/p>');
                        return false;
                    }
                }

                var $select =$("select[validate='empty']");
                for(var i=0;i<$select.length;i++){
                    if($select.eq(i).val().trim().length==0){
                        $("select[validate='empty']").eq(i).parent().after('<p class="le-tips"><span style="color: red;width: 100%">输入不能为空</span></p>');
                        return false;
                    }
                }
            },
            email:function(){
                $email=$("input[validate='email']").val();
                var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                if((typeof($email)!="undefined") && !re.test($email)){
                    $("input[validate='email']").after('<span class="le-tips"><span style="color: red;width: 100%">请输入正确的邮箱</span></p>');
                    return false;
                }
            },
            password: function () {
                $password = $("input[validate='password']").val();
                var re = /(.+){6,12}$/;
                if ((typeof($password) != "undefined") && !re.test($password)) {
                    $("input[validate='password']").after('<p class="le-tips"><span style="color: red;width: 100%">请输入6到12位密码</span></p>');
                    return false;
                }
            },
            //正整数
            positiveInteger: function () {
                $positiveInteger = $("input[validate='positiveInteger']");
                var re=/^[1-9]\d*|0$/;
                for (var i = 0; i < $positiveInteger.length; i++) {
                    var every=$positiveInteger.eq(i).val().trim();
                    if ((typeof(every) != "undefined") && !re.test(every)) {
                        $("input[validate='positiveInteger']").eq(i).after('<p class="le-tips"><span style="color: red">请输入一个正整数或者0</span></p>');
                        return false;
                    }
                }
            },
            decimal: function (value) {
                $decimal = $("input[validate='decimal']");
                var re = /^[0-9]+\.{0,1}[0-9]{0,2}$/;
                for (var i = 0; i < $decimal.length; i++) {
                    var every=$decimal.eq(i).val().trim();
                    if ((typeof(every) != "undefined") && !re.test(every)) {
                        $("input[validate='decimal']").eq(i).after('<p class="le-tips"><span style="color: red">值精确到0.01,不能小于0</span></p>');
                        return false;
                    }
                }
            }
         };


        var regularVerify = {
            mobile: function (value) {
                var re = /^1\d{10}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            email: function (value) {
                var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            password: function (value) {
                var re = /(.+){6,12}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            integer: function (value)     {
//                    var re = /^[1-9]\d*$/;
                var re = /^\d+$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            decimal: function (value) {
//                    var re = /^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$/;
                var re = /^[0-9]+\.{0,1}[0-9]{0,2}$/;
                if ((typeof(value) != "undefined") && !re.test(value)) {
                    return false;
                }
            },
            empty: function (value) {
                if (value.trim().length == 0) {
                    return false;
                }
            }
        }


        function leSubmit(){
        $("[le-submit]").click(function(){
            var sfunc = $("#form").attr("success-func");
            var bfunc = $("#form").attr("before-func");
            $("#form").ajaxSubmit({
                type:"post",
                dataType:"json",
                beforeSend:function(){
                    if(bfunc != null){
                        if(false == eval(bfunc)){
                            return false;
                        }
                    }
                    $(".le-tips").remove();
                    if(false == verification.empty()){
                        return false;
                    }
                    if(false == verification.mobiel()){
                        return false;
                    }
                    if(false == verification.email()){
                        return false;
                    }
                    if(false == verification.password()){
                        return false;
                    }
                    if(false == verification.positiveInteger()){
                        return false;
                    }
                },
                success:function(result){
                    if(sfunc != null){
                        eval(sfunc);
                        return;
                    }

                    if(result.isOk){
                        parent.isUpdateData = true;
                        parent.layer.closeAll();
                    }else{
                        layer.alert(result.message);
                    }
                },
                error:function(){
                    layer.alert("信息提交错误");
                }
            });
        })
}

            layerInit();
        leSubmit();

    //上传图片
    var upfun=null;
    var leImgurl ="";
    var leCdnImgUrl = "";
    $("[btn-leupload]").change(function(){
    upfun =  $(this).attr("le-uploadImg");
    })

    layui.upload({
    url: '/portal/attachment/upload',
    success: function(result){
    if(result.isOk){
    leImgurl = result.path;
    leCdnImgUrl = result.cdnPath;
    if (upfun != null) {
    eval(upfun);
    upfun=null;
    return;
    }
    $(".avatar").val(result.path);
    $(".le-avatar").attr("src",result.cdnPath);
    }else{
    alert(result.message);
    }
    },
    error:function(){
    alert("信息提交错误");
    }
    });



    $(document).on("click",".le-doSubmit",function(){

    $("#form").ajaxSubmit({
    type : "post", //提交方式
    dataType : "json", //数据类型
    beforeSubmit : function(){
    <!--$(".le-doSubmit").attr("disabled","disabled");-->
    <!--$(".le-doSubmit").attr('onclick','javascript:void(0);');-->
    },
    success : function(result){ //提交成功的回调函数
    if(result.isOk){
    alert(result.object);
    parent.isUpdateData = true;
    //location.reload();
    parent.layer.closeAll();
    }else{
    alert(result.message);
    }
    },
    error : function() {
    alert("信息提交错误");
    }
    });
    })


    </script>   
 </body>
</html>